window.onload = function () {
    var table = document.getElementById ('tableClickSort');
    var thead = table.getElementsByTagName('thead')[0];
    var tbody = table.tBodies[0];
    var rows = tbody.rows;
    var cells = thead.getElementsByTagName('th');
    var reg=/^[0-9]*$/;
    for ( var j = 0; j < cells.length; j++) {
        cells[j].onclick = function () {
            $('th').css("background-color","");
            $(this).css("background-color","#ddd");
            var asc = this.asc = !!this.asc ? -this.asc : -1;
            var array = [];
            array.index = this.cellIndex;
            for ( var i = 0; i < rows.length; i++) {
                array.push (rows[i]);
            }
            array.sort (function (a, b) {
                var n1 = a.cells[array.index].firstChild==null?"":a.cells[array.index].firstChild.nodeValue;
                var n2 = b.cells[array.index].firstChild==null?"":b.cells[array.index].firstChild.nodeValue;

                if(n1!=""&&reg.test(n1)){
                    n1=parseInt(n1);
                }
                if(n2!=""&&reg.test(n2)){
                    n2=parseInt(n2);
                }
                if (n1 > n2) {
                    return asc;
                } else if (n1 < n2) {
                    return -asc;
                } else {
                    return 0;
                }
            });
            for ( var i = 0; i < array.length; i++) {
                tbody.appendChild (array[i]);
            }
        }
    }
}



$(function(){
    var date = new Date();
    var currentdate = formatday(date);
    // var seperator1 = "-";
    // var month = date.getMonth() + 1;
    // var strDate = date.getDate();
    // if (month >= 1 && month <= 9) {
    //     month = "0" + month;
    // }
    // if (strDate >= 0 && strDate <= 9) {
    //     strDate = "0" + strDate;
    // }
    // var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    /*// $('#beginDate').val(currentdate);
    // $('#endDate').val(currentdate);*/


    $('#dadd').change(function () {
        if($('#dadd').val()==""){
            $('#beginDate').val("");
            $('#endDate').val("");
        }else {
            var beginDate=cc($('#dadd').val());
            $('#beginDate').val(formatday(beginDate));
            var endDate=new Date();
            $('#endDate').val(formatday(endDate));
        }
    });


    $('#tbody').on('dblclick','tr',function () {
        var stuuid=$(this).find('input[name=stu]').val();
        window.open("http://200.200.0.16:8089/wpacs.aspx?stuuid="+stuuid+"&user=999&pswd=999");
    })


    //条件查询
    $("#search").click(function(){
        console.log(1111111);
        var pid=$('#pid').val();
        var pname=$('#pname').val();
        var gender=$('#gender').val();
        var beginAge=$('#beginAge').val();
        var endAge=$('#endAge').val();
        var ageUnit=$('#ageUnit').val();
        var modality=$('#modality').val();
        var studesc=$('#studesc').val();
        var beginDate=$('#beginDate').val();
        var endDate=$('#endDate').val();

        console.log(pid+";"+pname+";"+gender+";"+beginAge+";"+endAge+";"+ageUnit+";"+modality+";"+studesc+";"+beginDate+";"+endDate);

        $.ajax({
            url:'/query?pid='+pid+'&pname='+pname+'&gender='+gender+'&beginAge='+beginAge+'&endAge='+endAge+'&ageUnit='+ageUnit
            +'&modality='+modality+'&studesc='+studesc+'&beginDate='+beginDate+'&endDate='+endDate,
            type:'get',
            dataType:'json',

            success:function (data) {
                // console.log(data);
                if(data.status=="success"){
                    // layer.alert("查询成功！");
                    $("#tbody").html("");
                    for(var i=0;i<data.data.length;i++){
                        var innerHtml="<tr>" +
                            "<td><input type='checkbox' name='stu' value='"+data.data[i].stuuid+"'/></td>" +
                            "<td>"+data.data[i].pid+"</td>" +
                            "<td>"+data.data[i].studateStr+" "+data.data[i].stutimeStr+"</td>" +
                            "<td>"+data.data[i].pname+"</td>" +
                            "<td>"+data.data[i].genderStr+"</td>" +
                            "<td>"+data.data[i].ageStr+"</td>" +
                            "<td>"+data.data[i].studescStr+"</td>" +
                            "<td>"+data.data[i].modality+"</td>" +
                            "<td>"+data.data[i].imagecount+"</td>" +
                            "</tr>";
                        // console.log(data.data[i])
                        $("#tbody").append(innerHtml);
                    }
                }else {
                    layer.alert("查询失败！");
                }

            }
        });
    });
});

function formatday(date) {
    var seperator1 = "-";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var dateStr = date.getFullYear() + seperator1 + month + seperator1 + strDate
    return dateStr;
}

function cc(dadd){
//可以加上错误处理
    var a = new Date()
    a = a.valueOf()
    a = a - dadd * 24 * 60 * 60 * 1000
    a = new Date(a)
    // console.log(a.getFullYear() + "年" + (a.getMonth() + 1) + "月" + a.getDate() + "日");
    return a;
}




function showpacs() {
    var codes =[]
    var count=[];
    $('input[name=stu]').each(function () {
        if($(this).is(":checked")){
            codes.push($(this).val());
            count.push('10%');
        }

    });
    // console.log(codes.length);
    if(codes.length>0) {
        var html="";
        for(var i=0;i<codes.length;i++) {
            // console.log(codes[i]);
            html+="<frame src='http://200.200.0.16:8089/wpacs.aspx?stuuid="+codes[i]+"&user=999&pswd=999'>";
            // window.open("http://demo.dayisheng.com/wpacs.aspx?stuuid="+codes[i]+"&user=cc&pswd=dd");
        }
        // console.log(html);

        // console.log("<html><frameset cols='"+count.toString()+"'>"+html+"</frameset></html>");
        var win=window.open("");
        win.document.write("<html><frameset cols='"+count.toString()+"'>"+html+"</frameset></html>");
    } else {
        layer.alert("选择要查看的用户！");
    }
}


function showaddpacs() {
    var codes =[]
    $('input[name=stu]').each(function () {
        if($(this).is(":checked")){
            codes.push($(this).val());
        }

    });
    // console.log(codes.length);
    if(codes.length>0) {
        // var html="";
        // for(var i=0;i<codes.length;i++) {
        //     // console.log(codes[i]);
        //     html+="<frame src='http://200.200.0.16:8089/wpacs.aspx?stuuid="+codes[i]+"&user=999&pswd=999'>";
        //     // window.open("http://demo.dayisheng.com/wpacs.aspx?stuuid="+codes[i]+"&user=cc&pswd=dd");
        // }

        window.open("http://200.200.0.16:8089/wpacs.aspx?stuuid="+codes.toString()+"&user=999&pswd=999");
    } else {
        layer.alert("选择要查看的用户！");
    }
}